package com.example.administrator.yuejian_2.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.yuejian_2.R;
import com.example.administrator.yuejian_2.adapter.CommentListAdapter;
import com.example.administrator.yuejian_2.bean.CommentAddBean;
import com.example.administrator.yuejian_2.bean.CommentListBean;
import com.example.administrator.yuejian_2.bean.PostDetailBean;
import com.example.administrator.yuejian_2.interservice.Const;
import com.example.administrator.yuejian_2.interservice.PostService;
import com.makeramen.roundedimageview.RoundedImageView;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class PostDetailActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private CommentListAdapter adapter;
    private RoundedImageView roundedImageView;
    private ImageView imageView1;
    private ImageView imageView2;
    private ImageView imageView3;
    private TextView uname;
    private TextView utitle;
    private TextView utime;
    private int pid;
    private int uid;
    private Context context;
    private EditText coadd;
    private Button coaddButton;
    private String con;
    private ImageButton backButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_postdetail);

        recyclerView = (RecyclerView) findViewById(R.id.postdetail_recyclerView);
        pid = getIntent().getIntExtra("pid",-1);

        SharedPreferences sharedPreferences = getSharedPreferences("userInfo",
                Context.MODE_PRIVATE);
        uid = sharedPreferences.getInt("user_id",0);

        roundedImageView = (RoundedImageView) findViewById(R.id.postdetail_userpic);
        imageView1 = (ImageView) findViewById(R.id.postdetail_imageView01);
        imageView2 = (ImageView) findViewById(R.id.postdetail_imageView02);
        imageView3 = (ImageView) findViewById(R.id.postdetail_imageView03);
        uname = (TextView) findViewById(R.id.postdetail_uname);
        utitle = (TextView) findViewById(R.id.postdetail_title);
        utime = (TextView) findViewById(R.id.postdetail_time);
        backButton = (ImageButton) findViewById(R.id.postdetail_imageButton);
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        getpostdetail();
        getcomment();

        coadd = (EditText) findViewById(R.id.postdetail_editText);
        coaddButton = (Button) findViewById(R.id.postdetail_button);
        coaddButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                con = coadd.getText().toString();
                addcomment();

            }
        });
    }

    private void getpostdetail(){
        context = PostDetailActivity.this;
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        PostService service = retrofit.create(PostService.class);
        Call<PostDetailBean> call = service.shaitudetail(pid);
        call.enqueue(new Callback<PostDetailBean>() {
            @Override
            public void onResponse(Call<PostDetailBean> call, Response<PostDetailBean> response) {
                if (response.body() != null){
                    String name = response.body().getU_name();
                    String upic = response.body().getU_pic();
                    String time = response.body().getTime();
                    String title = response.body().getTitle();
                    String pic1 = response.body().getPic1();
                    String pic2 = response.body().getPic2();
                    String pic3 = response.body().getPic3();

                    uname.setText(name);
                    utitle.setText(title);
                    utime.setText(time);
                    if (!upic.equals(""))
                        Picasso.with(context)
                                .load("http://10.0.2.2/YueJian16/Public/photo/"+upic)
                                .into(roundedImageView);
                    if (!pic1.equals(""))
                        Picasso.with(context)
                                .load("http://10.0.2.2/YueJian16/Public/photo/"+pic1)
                        .into(imageView1);
                    if (!pic2.equals(""))
                        Picasso.with(context)
                                .load("http://10.0.2.2/YueJian16/Public/photo/"+pic2)
                                .into(imageView2);
                    if (!pic3.equals(""))
                        Picasso.with(context)
                                .load("http://10.0.2.2/YueJian16/Public/photo/"+pic3)
                                .into(imageView3);



                }
            }

            @Override
            public void onFailure(Call<PostDetailBean> call, Throwable t) {

            }
        });
    }

    private void getcomment(){
        context = PostDetailActivity.this;
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        PostService service = retrofit.create(PostService.class);
        Call<List<CommentListBean>> call = service.getcomment(pid);
        call.enqueue(new Callback<List<CommentListBean>>() {
            @Override
            public void onResponse(Call<List<CommentListBean>> call, Response<List<CommentListBean>> response) {
                if (response.body() != null){
                    adapter = new CommentListAdapter(context,response.body());
                    LinearLayoutManager layoutManager = new LinearLayoutManager(context);
                    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                    recyclerView.setAdapter(adapter);
                    recyclerView.setLayoutManager(layoutManager);
                    recyclerView.setItemAnimator(new DefaultItemAnimator());
                }

            }

            @Override
            public void onFailure(Call<List<CommentListBean>> call, Throwable t) {

            }
        });


    }

    private void addcomment(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        PostService service = retrofit.create(PostService.class);
        Call<CommentAddBean> call = service.commentadd(pid,uid,con);
        call.enqueue(new Callback<CommentAddBean>() {
            @Override
            public void onResponse(Call<CommentAddBean> call, Response<CommentAddBean> response) {
                if (response.body().isInfo() == true){
                    Toast.makeText(getApplicationContext(),"评论成功",Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<CommentAddBean> call, Throwable t) {

            }
        });

    }
}
